package edu.zut.string_test;

import java.util.Arrays;

public class Demo3 {

    public static void main(String[] args) {

        String s = "hello";
        String s1 = " java";
        //在字符串末尾进行拼接
        s = s.concat(s1);
        System.out.println("s = " + s);
        //字符串是一组字符序列  长度  每个字符有下标
        int length = s.length();
        System.out.println("length = " + length);
        //根据下标找字符串中具体的元素
        char c = s.charAt(6);
        System.out.println("c = " + c);
//        eachString(s);
//        testSubString("78915456@qq.com");
        testSplit();
    }


    public static void testSplit(){
        String data = "1999-10-01";
        String[] strs = data.split("-");
        System.out.println(Arrays.toString(strs));
    }


    public static void testSubString(String mail){
        //78915456@qq.com
        String qq = mail.substring(0, mail.indexOf("@"));
        System.out.println("qq = " + qq);
    }


    /**
     * 判断字符串以xxx开头或结尾
     */
    public static void testCharArray(){
        String s = "hello_java";
        char[] array = s.toCharArray();
        System.out.println(Arrays.toString(array));

    }

    /**
     * 判断字符串以xxx开头或结尾
     */
    public static void testWith(){
        String s = "hello_java";
        boolean a = s.endsWith("a");
        System.out.println("a = " + a);
        boolean b = s.startsWith("he");
        System.out.println("b = " + b);

    }

    /**
     * 获取子字符串索引位置(从后往前第一次出现的位置)
     * 找不到返回-1
     */
    public static void testLastIndexOf(){
        String s = "hello_java";
        int i = s.lastIndexOf("a");
        System.out.println("i = " + i);
    }


    /**
     * 获取子字符串索引位置(从前往后第一次出现的位置)
     * 找不到返回-1
     */
    public static void testIndexOf(){
        String s = "hello_java";
        int i = s.indexOf("l");
        System.out.println("i = " + i);
    }


    /**
     * 遍历字符串
     * @param s
     */
    public static void eachString(String s){
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            System.out.print(c + " ");
        }
    }

}
